package com.snail.ddclock

import android.app.Application
import android.content.Context
import android.os.Handler
import android.util.Log
import com.snail.commons.AppHolder
import com.umeng.commonsdk.UMConfigure
import com.umeng.message.IUmengRegisterCallback
import com.umeng.message.PushAgent
import com.umeng.message.UmengMessageHandler
import com.umeng.message.entity.UMessage
import org.json.JSONObject
import java.util.*
import javax.mail.Address
import javax.mail.Session
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart


/**
 *
 *
 * date: 2019/7/23 09:49
 * author: zengfansheng
 */
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        AppHolder.init(this)
        UMConfigure.init(this, "589d7203b27b0a62c0000cdc", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "75518dae0c34aea7f1fae094b83e243b")
        //获取消息推送代理示例
        val mPushAgent = PushAgent.getInstance(this)
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(object : IUmengRegisterCallback {
            override fun onSuccess(deviceToken: String) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i(TAG, "注册成功：deviceToken：-------->  $deviceToken")
            }

            override fun onFailure(s: String, s1: String) {
                Log.e(TAG, "注册失败：-------->  s:$s,s1:$s1")
            }
        })
        val handler = Handler()
        val messageHandler = object : UmengMessageHandler() {
            override fun dealWithCustomMessage(context: Context, msg: UMessage) {
                Log.i(TAG, "自定义消息：-------->  $msg")
                handler.post {
                    try {
                        val json = JSONObject(msg.custom)
                        if (json["action"] == "com.alibaba.android.rimet") {
                            val i = packageManager.getLaunchIntentForPackage("com.alibaba.android.rimet")
                            if (i != null) {
                                context.startActivity(i)                                
                                sendTextMail()
                            }
                        }
                    } catch (e: Exception) {
                        Log.e(TAG, e.message, e)
                    }
                }
            }
        }
        mPushAgent.messageHandler = messageHandler
    }
    companion object {
        private const val TAG = "MyApplication"

        fun sendTextMail() {
            
        }
    }
}